package com.yunzheng.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yunzheng.common.BaseResult;
import com.yunzheng.dto.MissionChangeDTO;
import com.yunzheng.dto.MissionPostDTO;
import com.yunzheng.dto.UpdateMissionStatusDTO;
import com.yunzheng.entity.Project;
import com.yunzheng.entity.ProjectMission;
import com.yunzheng.exception.ServiceException;
import com.yunzheng.service.ProjectMissionService;
import com.yunzheng.util.SnowFlakeUtil;
import com.yunzheng.util.TokenParser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.time.Duration;
import java.util.List;

/**
 * @author 李美琪
 * @date 2020/7/27 21:56
 */

@Api(tags = "工作项")
@RestController
@RequestMapping("/mission")
public class ProjectMissionController {

    @Resource
    private ProjectMissionService projectMissionService;

    /**
     * 新建工作项
     *
     * /post -- authc
     */
    @ApiOperation(value = "1.新建工作项")
    @PostMapping("/post")
    public BaseResult missionPost(@Valid @RequestBody MissionPostDTO in){
        ProjectMission entity = new ProjectMission();
        BeanUtils.copyProperties(in, entity);
        entity.setMissionId(SnowFlakeUtil.nextId());
        long time = Duration.between(in.getStartTime(),in.getFinishTime()).toDays();
        if(time < 0){
            throw new ServiceException("结束时间早于开始时间");
        }
        projectMissionService.save(entity);
        return BaseResult.ok();
    }

    /**
     * 展示工作项列表
     *
     * /show/list -- authc
     */
    @ApiOperation(value = "2.展示工作项列表", notes = "missionType 1-需求,2-缺陷,default 3-任务")
    @GetMapping("/show/list")
    public BaseResult missionShowList(@RequestParam String projectId, @RequestParam Integer missionType){
        return BaseResult.ok(projectMissionService.missionShowList(projectId,missionType));
    }

    /**
     * 工作项甘特图页面
     *
     * /Ganttchart -- authc
     */
    @ApiOperation(value = "7.工作项甘特图页面")
    @GetMapping("/Ganttchart")
    public BaseResult missionGanttchart(@RequestParam String projectId){
        return BaseResult.ok(projectMissionService.missionGanttchart(projectId));
    }

//    @ApiOperation(value = "显示工作项内容")
//    @GetMapping("/show")
//    public BaseResult missionShow(@RequestParam String missionId){
//        return BaseResult.ok(projectMissionService.missionShow(missionId));
//    }

    /**
     * 修改工作项
     *
     * /change -- authc
     */
    @ApiOperation(value = "3.修改工作项")
    @PutMapping("/change")
    public BaseResult missionChange(@Valid @RequestBody MissionChangeDTO in){
        ProjectMission entity = new ProjectMission();
        BeanUtils.copyProperties(in, entity);
        entity.setMissionProgress(in.getProgress());
        // 判断起止时间是否合法
        long time = Duration.between(in.getStartTime(),in.getFinishTime()).toDays();
        if(time < 0){
            throw new ServiceException("结束时间早于开始时间");
        }
        projectMissionService.saveOrUpdate(entity);
        return BaseResult.ok();
    }

    /**
     * 删除工作项
     *
     * /remove -- authc
     */
    @ApiOperation(value = "4.删除工作项")
    @DeleteMapping("/remove")
    public BaseResult missionRemove(@RequestParam("missionId") String missionId){
//        projectMissionService.missionRemove(missionId);
        projectMissionService.removeById(missionId);
        return BaseResult.ok();
    }

    /**
     * 批量删除工作项
     *
     * /remove/list -- authc
     */
    @ApiOperation(value = "5.批量删除工作项")
    @DeleteMapping("/remove/list")
    public BaseResult missionRemoveList(@RequestParam List<String> missionId){
//        projectMissionService.missionRemoveList(missionId);
        projectMissionService.removeByIds(missionId);
        return BaseResult.ok();
    }

    /**
     * 我的工作项
     *
     * /mine -- authc
     */
    @ApiOperation(value = "6.我的工作项", notes = "priority-> 1-普通 10-比较重要 100-非常重要\n" +
            "missionType-> 1-需求 2-缺陷 3-任务")
    @GetMapping("/mine")
    public BaseResult myMission(HttpServletRequest request){
        return BaseResult.ok(projectMissionService.myMission(TokenParser.token2UserId(request)));
    }

    /**
     * 拖拽进行工作项状态转换
     *
     * /update -- authc
     */
    @ApiOperation(value = "8.拖拽进行工作项状态转换")
    @PutMapping("/update")
    public BaseResult updateMissionStatus(@Valid @RequestBody UpdateMissionStatusDTO input){
        projectMissionService.update(new UpdateWrapper<ProjectMission>().set(ProjectMission.COL_MISSION_TYPE, input.getMissionType()).eq(ProjectMission.COL_MISSION_ID, input.getMissionId()));
        return BaseResult.ok();
    }


}
